

###################################
## Merge results from all papers
###################################

### Please set path to the root folder of the project ###
# path <- "~/Dropbox/projectZ/IV Sensitivity/LLXZ_rep/" # please change this line
library(plyr)
library(dplyr)
library(readxl)
files.list <- list.files(paste0(path,"estimate/"))
files.list
name.list <- c("Acharya2016",     "Alt2015"  ,       "Arias2019"  ,   "Baccini2021" ,    "Barth2015",      
                 "Bhavnani2018",   "Blair2022"   ,    "Blattman2014",    "Carnegie2017" ,  "Charron2013" , 
                 "Charron2017" ,    "Chong2019"    , "Cirone2018"  ,    "Colantone2018a",  "Colantone2018b", 
                 "Coppock2016" ,    "Croke2016"     ,  "Delao2013"     , "Dietrich2015",    "Digiuseppe2022", 
                 "Dower2018a"    , "Dower2018b"  ,    "Dube2015"      ,  "Feigenbaum2015",  
                 "Flores2013"    ,  "Gelbach2012"   , "Gerber2010"    ,  "Goldstein2017" ,  "Grossman2017"  ,
                 "Hager2019_apsr",  "Hager2019ajps_a", "Hager2019ajps_b", "Hager2022"      , "Schubiger2021",
                 "Healy2013"      ,"Henderson2016_a", "Henderson2016_b", "Hong2022"       ,"Johns2016"      ,
                 "Kapoor2018"     , "Kim2019"        ,"Kocher2011"     , "Kriner2014"     , "Kuipers2022"    ,
                 "Laitin2016"     , "Lelkes2017"     , "Lerman2017"     ,"Lorentzen2014"  , "Lei2022",
                 "McClendon2014"  , "Meredith2013"   ,"Lopez2020"  , "Nellis2018"     , "Pianzola2019"   ,
                  "Ritter2016"     , "Rueda2017"      ,"Schleiter2016"  , "Sexton2019"     , "Spenkuch2018"   ,
                 "Stewart2017"    , "Stokes2016"     , "Tajima2013"     ,"Trounstine2016" , "Urpelainen2022" , 
                 "Vernby2013"     ,"Webster2022"    , "West2017", "Wood2022"       ,"Zhu2017"        , "Ziaja2020" )
length(name.list)

output.colnames <- c()
output <- as.data.frame(matrix(NA,nrow=0,ncol=length(output.colnames)))
colnames(output) <- output.colnames
for(name in name.list){
  sub.output <- as.data.frame(matrix(NA,nrow=1,ncol=length(output.colnames)))
  colnames(sub.output) <- output.colnames
  load(paste0(path,"estimate/",name,".RData"))
  sub.output$name <- name
  sub.output$ols_coef <- g$est_ols[1,1]
  sub.output$ols_analy_se <- g$est_ols[1,2]
  sub.output$ols_analy_ci1<- g$est_ols[1,4]
  sub.output$ols_analy_ci2<- g$est_ols[1,5]
  sub.output$ols_analy_p <- g$est_ols[1,6]

  sub.output$ols_bootc_se<-g$est_ols[2,2]
  sub.output$ols_bootc_ci1 <- g$est_ols[2,4]
  sub.output$ols_bootc_ci2<-g$est_ols[2,5]
  sub.output$ols_bootc_p<-g$est_ols[2,6]

  sub.output$ols_boott_ci1 <- g$est_ols[3,4]
  sub.output$ols_boott_ci2<-g$est_ols[3,5]
  sub.output$ols_boott_p<-g$est_ols[3,6]

  sub.output$iv_coef <- g$est_2sls[1,1]
  sub.output$iv_analy_se <- g$est_2sls[1,2]
  sub.output$iv_analy_ci1 <-g$est_2sls[1,4]
  sub.output$iv_analy_ci2 <-g$est_2sls[1,5]
  sub.output$iv_analy_p <-g$est_2sls[1,6]
    
  sub.output$iv_boot_se <-g$est_2sls[2,2]
  sub.output$iv_bootc_ci1 <- g$est_2sls[2,4]
  sub.output$iv_bootc_ci2 <-g$est_2sls[2,5]
  sub.output$iv_bootc_p <-g$est_2sls[2,6]
  
  sub.output$iv_boott_ci1 <- g$est_2sls[3,4]
  sub.output$iv_boott_ci2<-g$est_2sls[3,5]
  sub.output$iv_boott_p<-g$est_2sls[3,6]  

  sub.output$f_standard<-g$F_stat[[1]]
  sub.output$f_robust<-g$F_stat[[2]]
  sub.output$f_cluster<-g$F_stat[[3]]
  sub.output$f_boot<-g$F_stat[[4]]
  sub.output$f_effective<-g$F_stat[[5]]
  
  sub.output$tF_cF <- g$tF[[2]]
  sub.output$tF_p <- g$tF[[8]]

  sub.output$AR_F <- g$AR$Fstat[[1]]
  sub.output$AR_p <- g$AR$Fstat[[4]]
  sub.output$AR_bounded <- g$AR$bounded
  sub.output$AR_ci<-g$AR$ci.print
    
  sub.output$N <- g$N[[1]]
  sub.output$N_cl <- g$N_cl[[1]]
  sub.output$df <- g$df[[1]]
  sub.output$p_iv <-g$p_iv[[1]]
  sub.output$rho <-g$rho[[1]]
  sub.output$binaryD <- ifelse(g$nvalues[1, 2] == 2, 1, 0)
  sub.output$Zvalues <- ifelse(g$p_iv[[1]] == 1, g$nvalues[1, 3], max(g$nvalues[1, -c(1:2)]))

  print(name)
  output <- rbind.fill(output,sub.output)
}

iv.papers <- read.csv(paste0(path, "metadata/replicated.csv"))
dim(iv.papers)
d<-merge(iv.papers,output,by="name")
d$X <- NULL
d$X.1 <- NULL
names(d)

save(d, file = paste0(path, "metadata/iv_replicate.rds"))
dim(d)
